package net.lgpage.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.widget.LinearLayout;
import android.widget.TextView;

@SuppressLint("NewApi")
public class TextTouchView extends LinearLayout {

	public TextTouchView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	public TextTouchView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public TextTouchView(Context context) {
		super(context);
		init();
	}

	String[] datas = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S",
			"T", "U", "V", "W", "X", "Y", "Z" };

	private void init() {
		LayoutParams lp = new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1);
		for (int i = 0; i < datas.length; i++) {
			TextView tv = new TextView(getContext());
			tv.setLayoutParams(lp);
			tv.setText(datas[i]);
			tv.setTag(i);
			tv.setGravity(Gravity.CENTER);
			tv.setTextColor(0xff000000);
			tv.setTextSize(12f);
			this.addView(tv);
		}
	}

	public void setListener(OnItemTouchedListener listener) {
		this.listener = listener;
	}

	OnItemTouchedListener listener;

	public interface OnItemTouchedListener {
		public void onItemTouch(int position,int action);

	}

	@Override
	public boolean onTouchEvent(MotionEvent e) {
			int action = e.getAction();
		if (action == MotionEvent.ACTION_MOVE || action == MotionEvent.ACTION_DOWN
				|| action == MotionEvent.ACTION_UP) {
			float downy = e.getY();
			int h = this.getHeight();
			int ph = h / datas.length;
			int dy = (int) downy;
			int position = dy / ph;
			if (listener != null&&position>=0&&position<datas.length) {
					listener.onItemTouch(position,action);
			}

		}
		return true;
	}
}
